package com.example.booking.mapper;


import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.booking.domain.Patient;

/**
 * 数据操作：封装对数据库的操作
 * 
 * @author 植轶
 *
 */


//+---------+-------------------------+------+-----+---------+----------------+
//| Field   | Type                    | Null | Key | Default | Extra          |
//+---------+-------------------------+------+-----+---------+----------------+
//| id      | int                     | NO   | PRI | NULL    | auto_increment |
//| name    | varchar(16)             | YES  |     | NULL    |                |
//| id_card | char(18)                | NO   |     | NULL    |                |
//| phone   | char(11)                | NO   |     | NULL    |                |
//| state   | enum('正常','注销')        | NO   |     | 正常     |                |
//+---------+-------------------------+------+-----+---------+----------------+
@Mapper
public interface PatientMapper {
	//泛型、模板编程
	/**
	 * 按编号修改
	 * @param patient
	 */
	@Update("update patient set name=#{name},id_card=#{idCard},phone=#{phone} where id=#{id}")
	void updata(Patient patient);
	
	
	
	
	
	/**
	 * 按编号（id）删除就诊人
	 * @param id
	 */
//	@Delete("delete from patient where id = #{id}")
	@Update("update patient set state=2 where id = #{id}")
	void delete(int id);
	
	
	/**
	 * 向表中插入数据
	 * @param patient
	 */
	@Insert("insert into patient(name,id_card,phone) values(#{name},#{idCard},#{phone})")
	void create(Patient patient);
	
	
	
	/**
	 * 查询所有就诊人（未注销）的信息
	 * @return
	 */
	@Select("select id,name,id_card,phone from patient where state=1 order by id desc;")
//	@Results({
//		@Result(column = "id_card",property = "idCard")
//	})
	@ResultMap("patientMap")
	List<Patient> findAll();
	
	
	
	/**
	 * 根据id 查询就诊人信息
	 * @param id
	 * @return 就诊人或 null
	 */
	@Select("select id,name,id_card,phone from patient where id=#{id} and state=1;")
	@Results(
			id = "patientMap",
			value = {
				@Result(column = "id_card",property = "idCard")
			}
		)
	Patient findById(int id);
	
	
}
